package com.md.youyouhui.ui.user;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.md.youyouhui.R;
import com.md.youyouhui.databinding.ActivityOrderInfoBinding;
import com.md.youyouhui.databinding.AdapterOrderInfoBinding;
import com.md.youyouhui.net.response.GetOrderInfoResponse;
import com.md.youyouhui.ui.BaseActivity;
import com.md.youyouhui.ui.adapter.DataBoundViewHolder;
import com.md.youyouhui.utils.ToastUtils;
import com.md.youyouhui.view.MainViewModel;

import java.util.List;

/**
 * Created by AdministratorZJ
 * on 2019/5/17.
 */
public class OrderInfoActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {
    private ActivityOrderInfoBinding infoBinding;
    private MyAdapter adapter;
    private MainViewModel viewModel;

    @Override
    public void setContentLayout() {
        infoBinding = DataBindingUtil.setContentView(this, R.layout.activity_order_info);
        viewModel = new MainViewModel();
    }

    @Override
    public void initView() {
        setSupportActionBar(infoBinding.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public void initData() {
        adapter = new MyAdapter();
        infoBinding.orderRecycler.setAdapter(adapter);
        infoBinding.orderGroup.setOnCheckedChangeListener(this);
        getOrderInfo("0");
    }

    private void getOrderInfo(String type) {
        viewModel.getOrderInfo(type).observe(this, listResource -> {
            assert listResource != null;
            switch (listResource.status) {
                case LOADING:
                    infoBinding.progress.setVisibility(View.VISIBLE);
                    break;
                case SUCCESS:
                    infoBinding.progress.setVisibility(View.GONE);
                    List<GetOrderInfoResponse> data = listResource.data;
                    if (null == data) {
                        return;
                    }
                    if (data.size() == 0) {
                        Toast.makeText(ct, "暂无订单", Toast.LENGTH_SHORT).show();
                    }
                    adapter.upDate(data);
                    break;
                case ERROR:
                    infoBinding.progress.setVisibility(View.GONE);
                    ToastUtils.errMake(this, listResource.errorCode);
                    break;
                default:
                    break;
            }
        });
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.orderYiUse:
                getOrderInfo("0");
                break;
            case R.id.orderExpired:
                getOrderInfo("1");
                break;
            default:
                break;
        }
    }

    class MyAdapter extends RecyclerView.Adapter<DataBoundViewHolder<AdapterOrderInfoBinding>> {
        List<GetOrderInfoResponse> rows;

        public void upDate(List<GetOrderInfoResponse> rows) {
            this.rows = rows;
            notifyDataSetChanged();
        }

        @NonNull
        @Override
        public DataBoundViewHolder<AdapterOrderInfoBinding> onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            AdapterOrderInfoBinding userBinding = DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()), R.layout.adapter_order_info, viewGroup, false);
            return new DataBoundViewHolder<>(userBinding);
        }

        @Override
        public void onBindViewHolder(@NonNull DataBoundViewHolder<AdapterOrderInfoBinding> holder, int i) {
            /*holder.binding.orderAdapterNum.setText(String.format("订单号:%s", rows.get(i).credentials));
            holder.binding.orderAdapterTitle.setText(rows.get(i).title);
            holder.binding.orderAdapterPayTime.setText(String.format("购买日期:%s", rows.get(i).purchaseDate));
            holder.binding.orderAdapterEndTime.setText(String.format("开始日期:%s", rows.get(i).planDate));
            holder.binding.orderAdapterPrice.setText(rows.get(i).monthly);*/
            holder.itemView.setOnClickListener(v -> startActivity(new Intent(ct, OrderInfoListActivity.class)));
        }

        @Override
        public int getItemCount() {
            return null == rows ? 0 : rows.size();
        }
    }
}
